package se.joakimback.decorator;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import javax.imageio.ImageIO;

import se.joakimback.decorator.model.Layer;
import se.joakimback.decorator.renderer.RenderManager;
import se.joakimback.decorator.view.MainWindow;
import se.joakimback.decorator.view.Workspace;

public class Decorator {
	static final String sINPUT = "Image 01.png";

	private Layer newLayer() throws IOException {
		InputStream vSourceStream = this.getClass().getResourceAsStream(sINPUT);
		BufferedImage vSource = ImageIO.read(vSourceStream);

		Layer vLayer = new Layer();
		vLayer.setDensity(2);
		vLayer.setImage(vSource);
		vLayer.setRepeat(1);
		vLayer.setRotationVariance(0);
		vLayer.setBaseScale(1);
		vLayer.setScaleVariance(0.5);
		vLayer.setSpread(0);
		
		return vLayer;
	}
	
	private BufferedImage createBaseImage() {
		BufferedImage vImage = new BufferedImage(640, 640, BufferedImage.TYPE_INT_RGB);
		Graphics g = vImage.getGraphics();
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, 320, 320);
		
		return vImage;
	}

	public Decorator() throws IOException {
		List<Layer> vLayers = new LinkedList<Layer>();
		vLayers.add(newLayer());

		Workspace vWorkspace = Workspace.getSingleton();
		vWorkspace.setBaseImage(createBaseImage());
		vWorkspace.setLayers(vLayers);

		new MainWindow("Decorator");
		
		vWorkspace.setSelectedLayer(vLayers.get(0));
		
		RenderManager.getSingleton().request();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			new Decorator();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
